/*
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.i360day.invoker.registry;

import com.i360day.invoker.WebSocketHttpInvokerServer;
import com.i360day.invoker.common.BeanDefinitionRegistryUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;

import java.net.URI;
import java.util.Map;

/**
 * @author liju.z
 * @date 2024/3/19 22:38
 */
public class WebSocketRemoteScanImportBeanDefinitionRegistrar extends RemoteScanImportBeanDefinitionRegistrar {

    public WebSocketRemoteScanImportBeanDefinitionRegistrar(ResourceLoader resourceLoader, Environment environment) {
        super(resourceLoader, environment);
    }

    @Override
    protected void registerServerBean(BeanDefinitionRegistry registry, Class<?> interfaceClazz, Map<String, Object> attributes, Class<?> serverProxyClass) {
        //url
        URI serverUrl = getServerUrl(interfaceClazz, attributes, true);
        //已注册过，则忽略
        if (!BeanDefinitionRegistryUtils.containsBeanName(registry, serverUrl.getPath())) {
            BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(WebSocketHttpInvokerServer.class);
            beanDefinitionBuilder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
            beanDefinitionBuilder.addPropertyReference("service", BeanDefinitionRegistryUtils.getBeanName0(registry, interfaceClazz));
            beanDefinitionBuilder.addPropertyValue("serviceInterface", interfaceClazz);

            BeanDefinitionHolder beanDefinitionHolder = new BeanDefinitionHolder(beanDefinitionBuilder.getBeanDefinition(), serverUrl.getPath(), new String[]{interfaceClazz.getName()});
            registerBean(interfaceClazz, registry, beanDefinitionHolder);
        }
    }
}
